/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import beans.Bean;
import beans.EspecialidadBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Milene
 */
public class MSSQLEspecialidadDao extends MSSQLDao implements Dao{

    public MSSQLEspecialidadDao() throws Exception {
        
        super();
    }
    
    @Override
    public Bean make(ResultSet result) throws Exception {
       
        EspecialidadBean eb = new EspecialidadBean();
        eb.setNombreEspecialidad(result.getString("nombre"));
        eb.setDescripcionEspecialidad(result.getString("descripcion"));
        eb.setJefeEspecialidad(result.getString("jefe_especialidad"));
        return eb;
    }

    @Override
    public int insert(Bean bean) throws Exception {
       
         try
        {
        this.getConnection().setAutoCommit(false);
        this.setStatement("insertar_especialidad(?,?,?)");
        this.getStatement().setString(1, ((EspecialidadBean) bean).getNombreEspecialidad());
        this.getStatement().setString(2, ((EspecialidadBean) bean).getDescripcionEspecialidad());
        this.getStatement().setString(3, ((EspecialidadBean) bean).getJefeEspecialidad());
        this.getStatement().executeUpdate();

        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return 0;
    }

 
    @Override
    public int delete(Bean bean) throws Exception {
        int rs = 0;
        try {
            this.getConnection().setAutoCommit(false);//para mantener la integridad de la base, usar transaccion

            this.setStatement("eliminar_especialidad(?)");
            this.getStatement().setString(1, ((EspecialidadBean) bean).getNombreEspecialidad());

            this.getStatement().executeUpdate();
            
            this.getConnection().commit();

            
        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return rs;
    }

    @Override
    public List<Bean> select(Bean bean) throws Exception {
        
        ResultSet result;
        List<Bean> especialidades=new ArrayList<Bean>();
        this.setStatement("get_especialiddad");
        result=this.getStatement().executeQuery();
        while(result.next()){
            
            especialidades.add(this.make(result));
        }
        
        return especialidades;
    }
   

    @Override
    public boolean valid(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }   

    @Override
    public Bean get(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long getSize(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
